package com.jsd2412.backend.bed.service.impl;

import com.jsd2412.backend.bed.mapper.BedMapper;
import com.jsd2412.backend.bed.pojo.dto.*;
import com.jsd2412.backend.bed.pojo.entity.Bed;
import com.jsd2412.backend.bed.pojo.vo.BedVO;
import com.jsd2412.backend.bed.service.BedService;
import com.jsd2412.backend.patient.mapper.PatientMapper;
import com.jsd2412.backend.room.mapper.RoomMapper;
import com.jsd2412.backend.room.pojo.dto.RoomQuery;
import com.jsd2412.backend.room.pojo.vo.RoomVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class BedServiceImpl implements BedService {
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<BedVO> select(BedQuery bedQuery) {
        log.debug("查询床位业务:{}", bedQuery);
        List<BedVO> list = bedMapper.select(bedQuery);
        return list;
    }

    @Override
    public void save(BedSaveParam bedSaveParam) {
        log.debug("保存床位业务:{}", bedSaveParam);
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedSaveParam, bed);
        //获取房号对应的房间id
        Integer roomId = roomMapper.selectByNumber(bedSaveParam.getRoomNumber()).getId();
        bed.setRoomId(roomId);

        if (bed.getId() == null) {
            bed.setCreatedTime(new Date());
            log.debug("新增床位业务:{}", bed);
            bedMapper.insert(bed);
        } else {
            bed.setUpdatedTime(new Date());
            log.debug("修改床位业务:{}", bed);
            bedMapper.update(bed);
        }
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除床位业务:{}", id);
        bedMapper.delete(id);
    }

    @Override
    public List<BedVO> selectBedCard(BedQuery bedQuery) {
        log.debug("查询床位卡片信息业务:{}", bedQuery);

        //通过房号查出房间的id,楼层
        RoomQuery roomQuery = new RoomQuery();
        roomQuery.setNumber(bedQuery.getRoomNumber());
        RoomVO roomVO = roomMapper.select(roomQuery).get(0);
        Integer roomId = roomVO.getId();
        bedQuery.setRoomId(roomId);
        //通过状态和房号查询床位列表
        List<BedVO> bedList = bedMapper.select(bedQuery);

        //非空床位卡片信息列表
        List<BedVO> statusNotNullBedList = bedMapper.selectBedCardList(bedQuery);
        if ("已占用".equals(bedQuery.getStatus())) {
            return statusNotNullBedList;
        } else if ("空闲".equals(bedQuery.getStatus())) {
            return bedList;
        } else {
            TreeSet<BedVO> sortList = new TreeSet<>(Comparator.comparingInt(BedVO::getNumber));
            for (BedVO bedVO : bedList) {
                if (bedVO.getStatus().equals("空闲")) {
                    sortList.add(bedVO);
                }
            }
            sortList.addAll(statusNotNullBedList);

            return new ArrayList<>(sortList);
        }
    }

    @Override
    public List<BedVO> selectEmptyBedList(BedQuery bedQuery) {
        log.debug("查询指定房间空床业务：{}", bedQuery);
        return bedMapper.selectEmptyBedList(bedQuery);
    }

    @Override
    public void turnRoom(BedTurnParam bedTurnParam) {
        log.debug("转房业务：{}", bedTurnParam);
        //1.修改patient表中对应记录的bed_id,room_id
        //获取新房间id
        RoomQuery roomQuery = new RoomQuery();
        roomQuery.setNumber(bedTurnParam.getNewRoomNumber());
        Integer newRoomId = roomMapper.select(roomQuery).get(0).getId();
        //获取新床位id
        BedQuery bedQuery = new BedQuery();
        bedQuery.setNumber(bedTurnParam.getNewBedNumber());
        bedQuery.setRoomId(newRoomId);
        Integer newBedId = bedMapper.select(bedQuery).get(0).getBedId();
        //修改老人信息
        patientMapper.turnRoom(newRoomId, newBedId, bedTurnParam.getId());

        //2.将旧床位置空
        //获取旧房id
        roomQuery.setNumber(bedTurnParam.getRoomNumber());
        Integer roomId = roomMapper.select(roomQuery).get(0).getId();
        //更新旧床的状态
        bedMapper.updateStatus(null, bedTurnParam.getBedNumber(), roomId, "空闲");


        //3.修改新床位的状态
        bedMapper.updateStatus(null, bedTurnParam.getNewBedNumber(), newRoomId, "已占用");
    }

    @Override
    public void toEmpty(BedToEmptyParam bedToEmptyParam) {
        //修改老人信息
        patientMapper.turnRoom(null, null, bedToEmptyParam.getId());
        //将床位置空
        bedMapper.updateStatus(bedToEmptyParam.getBedId(), null, null, "空闲");
    }

    @Override
    public void allocation(BedAllocationParam bedAllocationParam) {
        Integer bedId = bedAllocationParam.getBedId();
        Integer roomId = bedAllocationParam.getRoomId();
        Integer id = bedAllocationParam.getId();
        patientMapper.turnRoom(roomId, bedId, id);
        bedMapper.updateStatus(bedId, roomId, null, "已占用");
    }

    @Override
    public int getBedTotalByRoom(String roomNumber) {
        RoomQuery roomQuery = new RoomQuery();
        roomQuery.setNumber(roomNumber);
        RoomVO roomVO = roomMapper.select(roomQuery).get(0);
        Integer roomId = roomVO.getId();
        return bedMapper.getBedTotalByRoom(roomId);
    }

    @Override
    public List<BedVO> selectBedList(BedQuery bedQuery) {
        log.debug("查询床位设置页显示的列表数据业务:{}", bedQuery);
        return bedMapper.selectBedList(bedQuery);
    }
}
